﻿using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.Windows.Media.Media3D;

namespace TestDxfDocument

{

    class xbs
    {
        //图F.62  ISYC 306-04-可转位刀片扩孔、平底锪钻
        public static void Xbs(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            // double sl_ = Convert.ToDouble(data["oal"]);
            // double scale = Convert.ToDouble(data["scale"]);
            double sl_ = 45;
            double scale = 1;
            if (sl_ <= 67)
            {
                scale = 4;
            }
            else if (67 < sl_ && sl_ <= 75)
            {
                scale = 2.5;
            }
            else if (75 < sl_ && sl_ <= 100)
            {
                scale = 2;
            }
            else
            {
                scale = 1;
            }

            // string b = "你好，世界,你好，世界";

            //float d=System.Diagnostics.Debug.WriteLine(a.Equals(b ));//打印
            //string d= Console.WriteLine( a.Equals(b))
            //Console.WriteLine("two line");
            /* void vis(double a)
             {   
                 if(a==null )
                 throw new NullReferenceException("没有值");
                 else
                 {
                     a = 1;
                     System.Diagnostics.Debug.WriteLine(a);
                 }
             }
             vis(b);*/


            /*
            double a = 10;
            double b = 0;
            for (a = 10; a > -1; a--)
            {
                if (b == 0)
                    throw new NullReferenceException("分母不能为零");
                if (a == 0)
                {
                    if (a.Equals(b))
                        throw new Exception("此算式中分子分母不能为零");
                }
                else { }
                if (a < 0 || b < 0)
                    throw new Exception("算式结果不能为负数");
                double c = a / b;
            }
            */
            ////////
            ///a%=pi相当与a=a%pi;
            /*
            Vector2 p0 = new Vector2(0, 0);
            Vector2 p1 = fun.Polar(p0, 0, 10);
            Vector2 p2 = fun.Polar(p1,46.565*pi/180, 5);

            var a = Vector2.Angle(p1,p2);
            System.Diagnostics.Debug.WriteLine(a) ;//打印
            a %= pi/2;
            double d = 180;
            d %= pi;
            System.Diagnostics.Debug.WriteLine(d );//打印
            double e = 180;
            e = e % pi;
            System.Diagnostics.Debug.WriteLine(e );//打印
            */
            ///////////
            ///

            //////////
            /////Clone()是创建一个空间且指向目标的作用
            /////////as是指判断线型的作用，"as运算符用于执行引用类型的显式类型转换。"若不一致则返回一个null，判断前需将线使用Clone()单独封装
            ///Vertexes是指把多段线里每个顶点都传入自变量中（例：三角形三个顶点）
            ///foreach是根据foreach括号后面的集合确定循环次数，而想要遍历集合需要用到in
            ///in就是用来指定要遍历的集合的，把集合名称放到in后面即可
            ///Position指 流中下一个读取或写入的位置，例：a6.Position += new Vector2 (0,30);
            //////////
            /*
            Vector2 p0 = new Vector2(0, 0);
            Vector2 p1 = fun.Polar(p0, 0, 10);
            Vector2 p2 = fun.Polar(p1, pi / 4, 10);
            Vector2 p3 = fun.Polar(p2, pi - pi / 4, 10);
            var polyline1 = Pus.AddPointsToLwPolyLine(p0, p1, p2, p3);
            Line line1 = new Line(p1, p2);
            var a = line1.Clone() as Line ;
            var b = polyline1.Clone() as Polyline;
            var c = polyline1.Vertexes;
            var polyline2= Pus.OffsetLwPolyline(polyline1, new Vector2 (-30,-10));
            var a1 = "yu";
            var a2 = "li";
            var a3 = a1 + a2;
            var a4 = "jojo";
            var after = polyline1.Clone() as LwPolyline ;
            var  a5 = after .Vertexes ;
            double i = 0;
            foreach (var a6 in a5)
            {
                i++;
                a6.Position += new Vector2 (0,30);
                System.Diagnostics.Debug.WriteLine(i);//打印
            }
            Vector2 p4 = fun.Polar(p0, pi / 3, 10);
            Vector2 p5 = fun.Polar(p4, pi / 3, 10);
            double[] p6 = { 1, 2, 3, 4, 5, 6, 7 };
            var polyline3 = Pus.AddPointsToLwPolyLine(p0, p4, p5,p0);
            var after1 = polyline3.Clone() as LwPolyline ;
            var vertexes = after1.Vertexes;
            double l = 0;
            foreach (var p7 in p6)
            {
                l+=2;
            }
            dxf.AddEntity(new EntityObject[] {
            });*/
            ///
            ///////////
            /////


            //////////
            ////上个方法实例
            //object p = new object { "jov", 2266156.2266 };
            /*
            List<object> list = new List<object>();
            list.Add("jov");
            list.Add(26516.165);
            list.Add(1656);
            list.Add("^&%$^$#&$*(^**");
            double i = 0;
            foreach (object item in list  )
            {
                Console.WriteLine(item );

            }
            List<int> Int = new List<int>();
            Int.Add(9561651);
            int b = 14566;
            bool  c=Int32.TryParse("解1放2军3", out b);
            Console.WriteLine(c);
            */
            /////
            //////////

            //////////
            //////continue:跳出本次循环
            /*
            double[] p1 = { 44554, 4, 646, 46, 56 };
            double p2 = p1.Length;
            System.Diagnostics.Debug.WriteLine(p2);//打印
            for (double i=0;i<10;i++)
            {
                if (i == 5)
                {
                    continue;
                }
                System.Diagnostics.Debug.WriteLine(i );//打印

            }*/
            //////
            ///

            ////////CrossProduct( , );是指向量的叉积计算，若值>0就是锐角，反之钝角，=0直角
            ///var crossProduct = Vector2.CrossProduct(basis, another);
            ///Vector2 ? p4 = null;
            ///?;1:代表判断比如 return（p>0）? 1:2;说的是若p大于0，就返还1，若p小于0则返还2
            ///?;2:代表空值，即连0都不是，pg：double? p1=null;那么p1就问空；double也可换成vector2等
            /*  Vector2 p0 = new Vector2(0, 0);
              Vector2 p1 = fun.Polar(p0, pi / 4, 10);

              var crossProduct = Vector2.CrossProduct(p0 , p1 );
              double p2;
              bool  age(int p, int pp = 10)
              {

                   p2 = p + pp;
                  return (p2 > 0) ? true : false ;
              }
              bool p3 = age(12, 12);
              System.Diagnostics.Debug.WriteLine(p3);//打印
              Vector2 ? p4 = null;

              var lwPolyline = Pus.AddPointsToLwPolyLine(p0, p1);
              var vetexes = lwPolyline.Vertexes;

              var afterReverse = new LwPolyline();
              var reverseM = new Matrix2(-1, 0, 0, 1);
              double i = 0;
              foreach (var vertex in vetexes)
              {
                  i++;
                  if (i == 1)
                  System.Diagnostics.Debug.WriteLine(vertex.Position);//打印
                  afterReverse.Vertexes.Add(new LwPolylineVertex(reverseM * vertex.Position, -vertex.Bulge));
              }
              afterReverse.Layer = lwPolyline.Layer;



              dxf.AddEntity(new EntityObject[] {
              lwPolyline,afterReverse 
              });


              //定义格式，根据路径导出
              dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
              dxf.Save(path);
          }


          */


            //GB/Z 29014.3 ISYC 302-02-整体阶梯钻
            /* var holeDia = 3.4;//通孔直径
             try
             {
                 holeDia = Convert.ToDouble(data["holedia"]);
             }
             catch (Exception e)
             {
                 Console.WriteLine("[holedia] not found");
             }

             Console.WriteLine(66666);
            *//*
            Vector2 p0 = new Vector2(0, 0);
            Vector2 p1 = fun.Polar(p0, pi / 4, 10);
            Vector2 p2 = fun.Polar(p1, pi - pi / 4, 10);
            //Line line1 = new Line(p0, p1);
            //Arc arc1 = fun.DrawArc(p1, fun.Polar(p0, pi / 4, 5), p0);
            var point1= Pus.AddPointsToLwPolyLine(p0, p1,p2);
            var point3 = new LwPolylineVertex( );
            //dxf.AddEntity(arc1);
            if (!point3  .Equals("(0,0)"))
            {
                p0 = new Vector2(0, 12) ;
            }
            System.Diagnostics.Debug.WriteLine(p0) ;
            System.Diagnostics.Debug.WriteLine(point3);

            var after = point1.Clone() as LwPolyline;
            var vec2 = point1.Vertexes;
            var vec3 = new LwPolyline();
            var a = new Matrix2(Math.Cos(120 * pi / 180), Math.Sin(120 * pi / 180), -Math.Sin(120 * pi / 180), Math.Cos(120 * pi / 180));
            var b = new Matrix2(4, 0, 0, 4);
            var c = a * b;
            foreach (var ac in vec2 )
            {
                vec3 .Vertexes.Add(new LwPolylineVertex(c * ac.Position , -ac.Bulge));
            }
            dxf.AddEntity(new EntityObject[] { point1, vec3 });
            int[] q = new int[6] ;
            */
            Vector2 p1 = new Vector2(0, 0);
            Vector2 p2 = fun.Polar(p1, pi / 3, 10);
            Vector2 p3 = new Vector2(4, 0);
            Vector2 p4 = fun.Polar(p3, 155 * pi / 180, 10);
            Vector2 p5 = fun.Polar(p4, 330 * pi / 180, 10);
            Vector2 p6 = new Vector2(p5.X, 10);

            Arc arc1 = fun.DrawArc4(p2, 5, p1, 0);
            var polyline1 = Pus.AddPointsToLwPolyLine(p1, p2, p3, p4, p5);
            var polyline2 = Pus.RotateByOrigin(pi / 2, polyline1);
            dxf.AddEntity(polyline2);

            dxf.AddEntity(new EntityObject[] { polyline1 });
            /* for (int i = 0; i < 5; i++)
             {
                 Vector2 v1001 = v1000[i];

                 Text txt1 = new Text("i" + i, v1001, 1.2, 0);
                 dxf.AddEntity(txt1);
             }*/


            //fun.seevector2(dxf, p1, p2, p3, p4, p5) ;
            /* int i = 0;
             int[] a1 = new int[201];
             for (int b = 0; b < 201; b++)
             {
                 a1[i] = b;
                 string c = a1[i].ToString();
                 if(c.Equals("3"))
                 {
                     System.Diagnostics.Debug.WriteLine(c);//打印
                 }

                 i++;
             }
             */



            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}